package com.tang.combine;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.ConnectedStreams;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.CoMapFunction;

/**
 * 聚合流 Connect
 * 1、一次只能连接两条流
 * 2、流的数据类型可以不同
 * 3、连接后可以调用 map、flatmap、process来处理，但是他们属于是各干各的，互不干扰
 *
 * @author tang
 * @since 2023/6/9 14:22
 */
public class ConnectDemo {

    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(new Configuration());
        env.setParallelism(1);

//        DataStreamSource<Integer> source1 = env.fromElements(1, 2, 3);
//        DataStreamSource<String> source2 = env.fromElements("a", "b", "c");

        SingleOutputStreamOperator<Integer> source1 = env.socketTextStream("192.168.70.141", 7777)
                .map(Integer::parseInt);
        DataStreamSource<String> source2 = env.socketTextStream("192.168.70.141", 8888);

        ConnectedStreams<Integer, String> connect = source1.connect(source2);

        SingleOutputStreamOperator<String> map = connect.map(new CoMapFunction<Integer, String, String>() {
            @Override
            public String map1(Integer value) throws Exception {
                return "来源于数字流" + value;
            }

            @Override
            public String map2(String value) throws Exception {
                return "来源于字母流" + value;
            }
        });
        
        map.print();

        env.execute();
        
    }
    
}
